package fm.jiecao.jiecaovideoplayer;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by Nathen
 * On 2016/02/07 01:01
 */
public class ListViewpagerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_viewpager);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setDisplayUseLogoEnabled(false);
    getSupportActionBar().setTitle("ListViewPagerDemo");

    List<View> listViews = new ArrayList<>();
    ListView listView1 = (ListView) getLayoutInflater().inflate(R.layout.layout_list, null);
    ListView listView2 = (ListView) getLayoutInflater().inflate(R.layout.layout_list, null);
    ListView listView3 = (ListView) getLayoutInflater().inflate(R.layout.layout_list, null);

    listView1.setAdapter(new VideoListAdapter(this));
    listView2.setAdapter(new VideoListAdapter(this));
    listView3.setAdapter(new VideoListAdapter(this));

    listViews.add(listView1);
    listViews.add(listView2);
    listViews.add(listView3);

    MyAdapter myAdapter = new MyAdapter(listViews);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(myAdapter);
    viewPager.setOnPageChangeListener(this);
  }

  @Override
  protected void onPause() {
    super.onPause();
    JCVideoPlayer.releaseAllVideos();
  }

  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

  }

  @Override
  public void onPageSelected(int position) {
    JCVideoPlayer.releaseAllVideos();
  }

  @Override
  public void onPageScrollStateChanged(int state) {

  }

  public class MyAdapter extends PagerAdapter {

    List<View> viewLists;

    public MyAdapter(List<View> lists) {
      viewLists = lists;
    }

    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return viewLists.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      // TODO Auto-generated method stub
      return arg0 == arg1;
    }

    @Override
    public void destroyItem(View view, int position, Object object) {
      ((ViewPager) view).removeView(viewLists.get(position));
    }

    @Override
    public Object instantiateItem(View view, int position) {
      ((ViewPager) view).addView(viewLists.get(position), 0);
      return viewLists.get(position);
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        finish();
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
